package com.example.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.common.R;
import com.example.domain.Employee;
import com.example.service.EmployeeService;
import jakarta.servlet.http.HttpServletRequest;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.DigestUtils;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;

@Slf4j
@RestController
@RequestMapping("/employee")
@ResponseBody
public class EmployeeController {
    @Autowired
    private EmployeeService employeeService;
    @PostMapping("/login")
    public R<Employee> login(HttpServletRequest request, @RequestBody Employee employee)//获取页面发送过来的数据
    {
        //对取得的密码进行MD5加密
        String password = employee.getPassword();
        password= DigestUtils.md5DigestAsHex(password.getBytes());

        //根据username查询数据库
        Employee emp = employeeService.selectemployeebyusername(employee.getUsername());

        //未查询到返回登录失败的结果
        if(emp==null)
        {
            return R.error("用户名不存在");
        }

        //密码比对，不一致则返回登录失败
        if(!emp.getPassword().equals(password))
        {
            return R.error("密码不正确");
        }

        //查看员工是否被禁用
        if(emp.getStatus()==0)
        {
            return R.error("员工被禁用");
        }

        //登录成功
        request.getSession().setAttribute("employee",emp.getId());
        return R.success(emp);
    }

    @PostMapping("/logout")
    public R<Employee> logout(HttpServletRequest httpServletRequest)
    {
        httpServletRequest.getSession().removeAttribute("employee");
        return R.success(null);
    }

    @PostMapping
    public R<Employee> addEmployee(@RequestBody Employee employee,HttpServletRequest request)
    {
//        Employee emp = employeeService.selectemployeebyusername(employee.getUsername());
//        if(emp!=null)
//        {
//            return R.error("用户名已存在");
//        }
//        else {
            employee.setPassword(DigestUtils.md5DigestAsHex("123456".getBytes()));
            Long id = (Long) request.getSession().getAttribute("employee");
            employee.setCreateUser(id);
            employee.setUpdateUser(id);
            employee.setCreateTime(LocalDateTime.now());
            employee.setUpdateTime(LocalDateTime.now());
            employee.setStatus(1);
            employeeService.addemployee(employee);
            return R.success(employee);
//        }
    }

    @GetMapping("/page")
    public R<Page> getMemberList(int page, int pageSize, String name)
    {
        //        IPage page=new Page(1,5);//第1页，每页有10条
//        userDao.selectPage(page,null);
//        page.getCurrent();//当前页数
//        page.getSize();//显示几条数据
//        page.getPages();//一共几页
//        page.getTotal();//一共几条数据
//        page.getRecords();//数据
        //IPage<Employee> page1=new Page<>(page,pageSize);
        //分页构造器
//        Page p=new Page(page,pageSize);
//
//        //条件构造器
//        LambdaQueryWrapper<Employee> lambdaQueryWrapper=new LambdaQueryWrapper();
//        //添加过滤条件
//        lambdaQueryWrapper.like(StringUtils.isNotEmpty(name),Employee::getName,name);
//        //添加排序
//        lambdaQueryWrapper.orderByDesc(Employee::getCreateTime);
//        //执行分页查询
//        employeeService.page(p,lambdaQueryWrapper);//把查询的对象放进p里面
//        //返回对象
//        return R.success(p);
        int pagecount=(page-1)*pageSize;
        Page<Employee> p=new Page(page,pageSize);
        if(name==null)
        {
            List<Employee> lists = employeeService.selectallbypage(pagecount, pageSize);
            p.setRecords(lists);
            long count = employeeService.selectcount();
            p.setTotal(count);
            log.info("查询了全部的信息");
            return R.success(p);
        }
        else
        {
            List<Employee> lists = employeeService.selectbypageandname(pagecount, pageSize, name);
            p.setRecords(lists);
            Long count=employeeService.selectcount();
            p.setTotal(count);
            log.info("查询了名字删选信息");
            return R.success(p);
        }
    }

    @PutMapping("/alert")
    public R<Employee> enableOrDisableEmployee(HttpServletRequest request,@RequestBody Employee employee){
        log.info("修改员工信息");
        employee.setUpdateTime(LocalDateTime.now());
        Long getId = (Long) request.getSession().getAttribute("employee");
        employee.setUpdateUser(getId);
        employeeService.updatestatusbyid(employee);
        return R.success(employee);
    }

    @GetMapping("/{id}")
    public R<Employee> getemployeebyid(@PathVariable Long id)
    {
        log.info("当前修改的员工id："+id);
        Employee selectemployeebyid = employeeService.selectemployeebyid(id);
        if(selectemployeebyid==null)
        {
            return R.error("无对应员工");
        }
        return R.success(selectemployeebyid);
    }
    @PutMapping
    public R<Employee> editEmployee(HttpServletRequest request,@RequestBody Employee employee)
    {
        Long getId = (Long) request.getSession().getAttribute("employee");
        log.info("当前修改的id"+getId);
        employee.setUpdateTime(LocalDateTime.now());
        employee.setUpdateUser(getId);
        employeeService.updateemployee(employee);
        return  R.success(employee);
    }


}
